// 上传文件

var count = 0;
var dataArr = [];
//服务端接口路径
var server = Config.upurl + "/api/v2/sbxj/upload";
var AppId = mui.baseApp.AppId; //APPid
var AppKey = mui.baseApp.AppKey; // AppKey
var AppSecret = mui.baseApp.AppSecret; // AppSecret
var Version=localStorage.getItem('version'); // 版本
var timestamp = Date.parse(new Date()); // 时间戳
var OSType; // 系统
var DGUID;
var alllength;
var offtype = '';
var sbitem;
//	console.log("图片地址。。" + JSON.stringify(saveimg));
function upload(path, j, length, i,type) {
	// var wt = plus.nativeUI.showWaiting();
	var task = plus.uploader.createUpload(server + "?AppId=" + AppId + "&AppKey=" + AppKey + "&AppSecret=" + AppSecret + "&version=" + Version + "&timestamp=" + timestamp + "&OSType=" + OSType + "&DGUID=" + DGUID, {
			method: "POST"
		},
		function(t, status) { //上传完成
			if(status == 200) {
				console.log("上传成功：" + t.responseText);
				if(JSON.parse(t.responseText).status == 200) {
					var date = JSON.parse(t.responseText);
					issavePath = date.data.savePath;
					var up_img = {
						j: j,
						issavePath: issavePath
					}
					count++;
					console.log('上传路径：' + issavePath);
					dataArr.push(up_img);
					localStorage.setItem('saveimg', JSON.stringify(dataArr));
					console.log("图片地址1。。" + JSON.stringify(dataArr));
					console.log("count。。" + count);
					console.log("length。。" + length);
					
					if(count == length) {
						if(type == 'all'){
							_addata(i);
						}else if(type == 'single'){
							addata(i);
						}
					}

				} else if(JSON.parse(t.responseText).status == 400) {
					mui.toast(JSON.parse(t.responseText).message);
					// wt.close();
					return;
				} else {
					// wt.close();
					return;
				}
				// wt.close(); //关闭等待提示按钮
			} else {
				console.log("上传失败：" + status);
				// wt.close(); //关闭等待提示按钮
				mui.toast("上传失败");
			}
		}

	);
	//			//添加其他参数
	task.addData("file", path);
	task.addFile(path, {
		key: path
	});
	task.start();
	//	console.log('status3'+issavePath);
}

function allTijiao(){
	if (plus.networkinfo.getCurrentType()  ==  plus.networkinfo.CONNECTION_NONE)  {
		mui.toast('当前无网络不能提交')
	} else {
		var btnArr = ['确认', '取消'];
		mui.confirm('确认提交吗?', '操作提示', btnArr, function(e) {
			if(e.index == 0) {
				var save = JSON.parse(localStorage.getItem("savexcxj"));
				alllength = save.length;
				if(alllength == 0){
					mui.toast('暂无数据')
					return;
				}
				plus.nativeUI.showWaiting('请稍后');
				_allTijiao();
			} else {
				console.log('取消')
			}
		});
	}
}

function _allTijiao(){
	var save = JSON.parse(localStorage.getItem("savexcxj"));
	if(!save || save.length == 0){
		return;
	}
	var jcx = save[0].jcxArray;
	var n = 0;
	console.log('jcx-----:' + JSON.stringify(jcx));
	for(var m = 0; m < jcx.length; m++) {
		var src = jcx[m].attach;

		if(src == "") {
			n++;
		}
	}
	var length = jcx.length - n;
	if(n == jcx.length) {
		_addata(0);
	} else {
		for(var j = 0; j < jcx.length; j++) {
			var src = jcx[j].attach;

			if(src == "") {

			} else {
				upload(src, j, length, 0,'all');
			}
		}

	}
}

function buttontijiao() {
	mui('.submitparent').on('tap', '.btn-submit', function() {
		sbitem = this.getAttribute('data-item');
		console.log('sbitem======'+sbitem);
		console.log('offtype======'+offtype);
		sbitem = JSON.parse(sbitem);
		if (plus.networkinfo.getCurrentType()  ==  plus.networkinfo.CONNECTION_NONE)  {

			mui.toast('当前无网络不能提交')
		} else {
			var xjlbid = sbitem.number;
			var rate = sbitem.rate;
			var save = JSON.parse(localStorage.getItem("savexcxj"));
			// console.log('save-----:' + JSON.stringify(save));
			for(var i = 0; i < save.length; i++) {
				console.log('xjlbid='+xjlbid);
				if(xjlbid == save[i].xjxArray.scancode && rate == save[i].index) {
					var jcx = save[i].jcxArray;
					var n = 0;
					console.log('jcx-----:' + JSON.stringify(jcx));
					for(var m = 0; m < jcx.length; m++) {
						var src = jcx[m].attach;

						if(src == "") {
							n++;
						}
					}
					var length = jcx.length - n;
					if(n == jcx.length) {
						addata(i);
					} else {
						for(var j = 0; j < jcx.length; j++) {
							var src = jcx[j].attach;

							if(src == "") {

							} else {
								upload(src, j, length, i,'single');
							}
						}

					}
				}

			}

		}
	})
}

//上传图片后提交数据 
var _isnormal=0;
function _addata(index) {
	var save = JSON.parse(localStorage.getItem("savexcxj"));
	var saveimg = JSON.parse(localStorage.getItem('saveimg'));
	console.log("有网图片地址。。" + JSON.stringify(saveimg));
	var list = save[0].jcxArray;
	var xjlbid = save[0].userxjId;
	var rate = save[0].xjxArray.index;
	var scancode = save[0].xjxArray.scancode;
	var inspect_time = save[0].xjxArray.smTime;
	var rate = save[0].index;
	console.log("获取提交数据。。" + JSON.stringify(list));
	var arr = [];
	for(var i = 0; i < list.length; i++) {
		var obj = {};
		obj.desc = list[i].desc;
		if(obj.desc!==""){
			_isnormal=1;
		}
		obj.id = list[i].id;
		obj.state = list[i].state;
		var attach = "";
		if(saveimg == null) {

		} else {
			for(var m = 0; m < saveimg.length; m++) {
				if(saveimg[m].j === i) {
					attach = saveimg[m].issavePath;
				}
			}
		}

		obj.attach = attach;
		arr.push(obj);
	}
	var jcxArray = JSON.stringify(arr);
	console.log('提交前数据' + JSON.stringify(arr));
	console.log(jcxArray);
	plus.nativeUI.closeWaiting();
	plus.nativeUI.showWaiting('提交中'+(alllength - save.length + 1)+'/'+alllength);
	console.log('111111');
	localStorage.removeItem('saveimg');
	mui.baseApp.httpRequst('/api/v2/sbxj/submitUserXj', {
		memberId: localStorage.getItem('userid'),
		rate: rate,
		scancode: scancode,
		inspect_time: inspect_time,
		isnormal: _isnormal,
		jcxArray: jcxArray
	}, function(data) {
		if(save.length == 1){
			plus.nativeUI.closeWaiting();
			plus.webview.getWebviewById("record_sub.html").evalJS('pulldown()');
			var page = plus.webview.getWebviewById("jiankong.html");
			mui.fire(page, 'home');
		}
		console.log('111111' + JSON.stringify(data));
		if(data.status == "200") {
			// mui.toast(data.message);
			draglistremove(xjlbid);
			console.log('1111');
			// local();
			_allTijiao();

		} else if(data.status == "400") {
			mui.toast(data.message);
		} else if(data.status == "409") {
			mui.toast(data.message);
		}
	})

}

//上传图片后提交数据
function addata(i) {
	var isnormal = 0;
	var save = JSON.parse(localStorage.getItem("savexcxj"));
	var saveimg = JSON.parse(localStorage.getItem('saveimg'));
	console.log("有网图片地址。。" + JSON.stringify(saveimg));
	var list = save[i].jcxArray;
	var xjlbid = save[i].userxjId;
	// var rate = save[i].xjxArray.index;
	var scancode = save[i].xjxArray.scancode;
	var inspect_time = save[i].xjxArray.smTime;
	var rate = save[i].index;
	// console.log("获取提交数据。。" + JSON.stringify(list));
	var arr = [];
	for(var i = 0; i < list.length; i++) {
		var obj = {};
		obj.desc = list[i].desc;
		if(obj.desc!==""){
			isnormal=1;
		}
		obj.id = list[i].id;
		obj.state = list[i].state;
		var attach = "";
		if(saveimg == null) {

		} else {
			for(var m = 0; m < saveimg.length; m++) {
				if(saveimg[m].j === i) {
					attach = saveimg[m].issavePath;
				}
			}
		}

		obj.attach = attach;
		arr.push(obj);
	}
	var jcxArray = JSON.stringify(arr);
	// console.log('提交前数据' + JSON.stringify(arr));
	// console.log(jcxArray);

	var btnArr = ['确认', '取消'];
	mui.confirm('确认提交吗?', '操作提示', btnArr, function(e) {
		if(e.index == 0) {
			console.log('.......111111.......');
			console.log('userid='+localStorage.getItem('userid'));
			console.log('rate='+rate);
			console.log('userxjId='+sbitem.id);
			console.log('scancode='+scancode);
			console.log('inspect_time='+inspect_time);
			console.log('isnormal='+isnormal);
			console.log('jcxArray='+jcxArray);
			localStorage.removeItem('saveimg');
			plus.nativeUI.showWaiting('提交中...');
			mui.baseApp.httpRequst('/api/v2/sbxj/submitUserXj', {
				memberId: localStorage.getItem('userid'),
				rate: rate,
				userxjId: sbitem.id,
				scancode: scancode,
				inspect_time: inspect_time,
				isnormal: isnormal,
				jcxArray: jcxArray
			}, function(data) {
				plus.nativeUI.closeWaiting();
				console.log('111111' + JSON.stringify(data));
				if(data.status == "200") {
					mui.toast(data.message);
					if(offtype == 'shebei'){
						deleteSbList(scancode,rate);
						draglistremove(scancode,rate);
					}else{
						if(offtype == 'pinci'){
							var pcchild = plus.storage.getItem('pcchild') || '[]';
							pcchild = JSON.parse(pcchild);
							for (var i = 0; i < pcchild.length; i++) {
								if(pcchild[i].key == xsdwindid){
									var arr = pcchild[i].value;
									for (var j = 0; j < arr.length; j++) {
										if(arr[j].number == sbitem.number){
											arr[j].flags = false;
											pcchild[i].value = arr;
										}
									}
								}
							}
							plus.storage.setItem('pcchild',JSON.stringify(pcchild));
						}else if(offtype == 'xianlu'){
							var xlchild = plus.storage.getItem('xlchild') || '[]';
							xlchild = JSON.parse(xlchild);
							for (var i = 0; i < xlchild.length; i++) {
								if(xlchild[i].key == xsdwindid){
									var arr = xlchild[i].value;
									for (var j = 0; j < arr.length; j++) {
										if(arr[j].number == scancode){
											arr[j].flags = false;
											xlchild[i].value = arr;
										}
									}
								}
							}
							plus.storage.setItem('xlchild',JSON.stringify(xlchild));
						}
						getData();
					}
					// plus.webview.getWebviewById("record_sub.html").evalJS('pulldown()');
					var page = plus.webview.getWebviewById("jiankong.html");
					mui.fire(page, 'home');
					console.log('1111');
					// local();

				} else if(data.status == "400") {
					mui.toast(data.message);
				} else if(data.status == "409") {
					mui.toast(data.message);
				}

			})
		} else {
			localStorage.removeItem('saveimg');
			count=0;
			dataArr.splice(0,dataArr.length);
			console.log('取消')
		}

	});

}

//提交成功后判断草稿箱中是否有，有的话删除
function deleteSbList(scancode,rate) {
	var sb = plus.storage.getItem('sb');
	console.log('设备.....'+JSON.stringify(sb));
	sb = JSON.parse(sb);
	for(var m = 0; m < sb.length; m++) {
		if(sb[m].number == scancode && sb[m].rate == rate) {
			sb.splice(m, 1);
			vm.data1 = sb;
			plus.storage.setItem('sb', JSON.stringify(sb));
		}
	}
}

//提交成功后判断草稿箱中是否有，有的话删除
function draglistremove(scancode,rate) {
	var save = JSON.parse(localStorage.getItem("savexcxj"));

	for(var m = 0; m < save.length; m++) {
		if(save[m].xjxArray.scancode == scancode && save[m].index == rate) {
			save.splice(m, 1);
			console.log("提交后的草稿箱数组1068。。" + JSON.stringify(save))
			localStorage.setItem("savexcxj", JSON.stringify(save));
		}
	}
}